package com.example.coder_voucher.config;

import com.example.coder_voucher.entity.User;
import com.example.coder_voucher.entity.Voucher;
import com.example.coder_voucher.repository.UserRepository;
import com.example.coder_voucher.repository.VoucherRepository;
import com.example.coder_voucher.service.VoucherInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class DataInitializer implements CommandLineRunner {
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private VoucherRepository voucherRepository;
    
    @Autowired
    private VoucherInventoryService voucherInventoryService;
    
    @Override
    public void run(String... args) throws Exception {
        createTestUsers();
        createTestVouchers();
        initializeRedisInventory();
        System.out.println("Test data initialized successfully!");
    }
    
    private void createTestUsers() {
        if (userRepository.count() == 0) {
            User user1 = new User("john_doe", "john@example.com", "John Doe", "1234567890");
            User user2 = new User("jane_smith", "jane@example.com", "Jane Smith", "0987654321");
            User user3 = new User("admin_user", "admin@example.com", "Admin User");
            
            userRepository.save(user1);
            userRepository.save(user2);
            userRepository.save(user3);
            
            System.out.println("Created 3 test users");
        }
    }
    
    private void createTestVouchers() {
        if (voucherRepository.count() == 0) {
            LocalDateTime now = LocalDateTime.now();
            
            Voucher voucher1 = new Voucher(
                "SAVE10", "10% Off Everything", "Get 10% discount on all items",
                new BigDecimal("10"), "PERCENTAGE", new BigDecimal("50.00"),
                100, now.minusDays(1), now.plusDays(30)
            );
            
            Voucher voucher2 = new Voucher(
                "FIXED5", "$5 Off Purchase", "Get $5 off your purchase",
                new BigDecimal("5.00"), "FIXED", new BigDecimal("25.00"),
                50, now.minusDays(1), now.plusDays(15)
            );
            
            voucherRepository.save(voucher1);
            voucherRepository.save(voucher2);
            
            System.out.println("Created 2 test vouchers");
        }
    }
    
    private void initializeRedisInventory() {
        List<Voucher> vouchers = voucherRepository.findAll();
        for (Voucher voucher : vouchers) {
            try {
                voucherInventoryService.initVoucherInventory(voucher);
                System.out.println("Initialized Redis inventory for voucher: " + voucher.getCode());
            } catch (Exception e) {
                System.err.println("Failed to initialize Redis inventory for voucher " + voucher.getCode() + ": " + e.getMessage());
            }
        }
    }
}